package com.qzdsoft.erpcloud.controller.stock;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.domain.stock.StockStore;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.proj.MaterialService;
import com.qzdsoft.erpcloud.service.stock.StockInboundDetailService;
import com.qzdsoft.erpcloud.service.stock.StockInboundService;
import com.qzdsoft.erpcloud.service.stock.StockStoreService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.proj.MaterialInfo;
import com.qzdsoft.erpcloud.vo.proj.MaterialQueryInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundDetailEditInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundDetailEditItemInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundDetailInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundDetailQueryInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundDetailRemoveInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundInfo;
import com.qzdsoft.utils.ExceptionUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.proj.MaterialReviewStatus;

@Controller
@RequestMapping("/stock/inbound/detail")
public class StockInboundDetailController {
    
    private static final String BASEPATH = "stock/inbounddetail";
    private static final Logger logger = LoggerFactory.getLogger(StockInboundDetailController.class);
    
    @Autowired
    private StockInboundService stockInboundService;
    @Autowired
    private CustomerSupplierService customerSupplierService;
    @Autowired
    private MaterialService materialService;
    @Autowired
    private StockInboundDetailService stockInboundDetailService;
    @Autowired
    StockStoreService stockStoreService;
    /**
     * 明细列表页面
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/index")
    public ModelAndView index(Integer id) {
    	//查询采购入库单
    	StockInboundInfo  info= stockInboundService.findStockInboundOrderBy(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("StockInboundOrder", info);
      /*  List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status",status);*/
        mv.setViewName(BASEPATH+"/index");
        return mv;
    }
    /**
     * 明细数据
     * @param id
     * @param queryInfo
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/order-details")
    public ModelAndView item(StockInboundDetailQueryInfo queryInfo) {
    	logger.debug("采购订单明细:id={},companyId={}",queryInfo.getId(),queryInfo.getCompanyid());
    	logger.debug("采购订单明细:materiaCode={}",queryInfo.getMateriaCode());
    	ModelAndView mv = new ModelAndView();
    	 //查询物料信息
        MaterialQueryInfo info = new MaterialQueryInfo();
        info.setIsBuy(false);//入库物料
        info.setStatus(MaterialReviewStatus.HAS_REVIEW_CODE);//查询已经审核的物料
        List<MaterialInfo> materialInfos = materialService.selectBy(info);
    	mv.addObject("materialInfos", materialInfos);
    	//查询采购入库单
    	StockInboundInfo  InboundOrder= stockInboundService.findStockInboundOrderBy(queryInfo.getId());
    	logger.debug("InboundOrder:id={}",InboundOrder.getId());
    	mv.addObject("InboundOrder", InboundOrder);
    	//公司仓库信息
    	List<StockStore> stockStore = stockStoreService.selectByComId(queryInfo.getCompanyid());
        mv.addObject("stockStore",stockStore);
    	//出库库单明细
    	List<StockInboundDetailInfo> details = stockInboundDetailService.selectDetailBy(queryInfo);
    	mv.addObject("details", details);
    	mv.setViewName(BASEPATH + "/detailItem");
    	return mv;
    }
    /**
     * 明细移除
     * @return
     */
    @Permission(validate=false)
    @SysLog(description="采购入库单明细移除")
    @ResponseBody
    @RequestMapping("/remove")
    public Response<String> remove(@RequestBody StockInboundDetailRemoveInfo removeInfo) {
    	try{
    		logger.debug("采购入库订单详情删除，ids：{}",removeInfo.getIds());
    		return stockInboundDetailService.remove(removeInfo.getIds());
    	}catch(Exception ex){
    		 logger.error("编辑采购订单明细错误，错误信息：{}", ExceptionUtil.getStackTrace(ex));
    	}
    		return new Response<String>(ResultEnum.ERROR);
    	
    }
    /**
     * 明细新增保存
     * @param info
     * @return
     */
    @Permission(validate=false)
    @SysLog(description="采购入库单明细保存")
    @ResponseBody
    @RequestMapping("/edit-orderdetail")
    public Response<String> edit(@RequestBody StockInboundDetailEditInfo info) {
       logger.debug("明细保存:",info.getBillid(),info.getBillno(),info.getCompanyid(),info.getSourceOrderId(),info.getSourceOrderNo(),info.getSupplierid());
        try
        {
        	return stockInboundDetailService.edit(info);
//            return purchaseOrderDetailService.edit(info);
//        	return new Response<String>(ResultEnum.SUCCESS);
        }
        catch (Exception ex)
        {
            logger.error("编辑采购订单明细错误，错误信息：{}", ExceptionUtil.getStackTrace(ex));
        }
        return new Response<String>(ResultEnum.ERROR);
    }
    /**
     * 查询物料信息
     * @param info
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/materias")
    public LayTableResponse<MaterialInfo> list(MaterialQueryInfo info){
        info.setIsBuy(true);
        info.setStatus(MaterialReviewStatus.HAS_REVIEW_CODE);//查询已经审核的物料
        return materialService.selectPageBy(info);
    }
    
    /**
     * 物料选择页面
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/selectmaterials")
    public ModelAndView selectMaterial() {
        ModelAndView view = new ModelAndView();
        view.setViewName(BASEPATH+"/materials");
        return view;
    }
   /* @ResponseBody
    @RequestMapping("/list")
    public LayTableResponse<StockInboundInfo> list(StockInboundQueryInfo info){
        return stockInboundService.findStockInboundOrdersBy(info);
    }
    
    @RequestMapping("/{companyid}/{id}")
    public ModelAndView toedit(@PathVariable("companyid")String companyid,@PathVariable("id")Integer id) {
        logger.debug("查询采购入库单详情：companyid={},id={}",companyid,id);
        ModelAndView mv = new ModelAndView();
        StockInboundInfo order = stockInboundService.findStockInboundOrderBy(id);
        mv.addObject("order", order);
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status",status);
        mv.setViewName(BASEPATH+"/edit");
        return mv;
    }
    @ResponseBody
    @RequestMapping("/edit")
    public Response<StockInboundInfo> edit(StockInbound info) {
        logger.info(info.getId()+","+info.getRemark());
        logger.debug("采购入库单编辑:id={}",info.getId());
        return stockInboundService.edit(info);
    }
    
    @RequestMapping("/{companyid}")
    public ModelAndView toadd(@PathVariable("companyid")Integer companyid) {
    	logger.debug("采购入库单新增：companyid={}",companyid);
    	logger.debug(companyid.toString());
    	
    	ModelAndView mv = new ModelAndView();
    	mv.setViewName(BASEPATH+"/add");
    	return mv;
    }
    *//**
     * 新增表头保存
     * @param info
     * @return
     *//*
    @ResponseBody
    @RequestMapping("/save")
    public Response<String> addSave(StockInbound info) {
    	logger.info(info.getBillNo()+":"+info.getRemark());
    	logger.debug("采购入库单新增保存:");
    	
    	return stockInboundService.save(info);
    }
    
    *//**
     * 采购入库删除
     * @param info
     * @return
     *//*
    @ResponseBody
    @RequestMapping("/del/{id}")
    public Response<String> del(@PathVariable("id")Integer id) {
//    	logger.info(info.getBillNo()+":"+info.getRemark());
    	logger.debug("采购入库单删除:id={}",id);
    	return stockInboundService.del(id);
    }
    
    @RequestMapping("/view/{id}")
    public ModelAndView view(@PathVariable("id")Integer id) {
    	logger.debug("采购入库单据查看:id={}",id);
    	ModelAndView mv = new ModelAndView();
    	StockInboundInfo order = stockInboundService.findStockInboundOrderBy(id);
        mv.addObject("order", order);
        mv.setViewName(BASEPATH+"/view");
    	return mv;
    }*/
}
